/*
 * @Author: shanghongyun 2879455630@qq.com
 * @Date: 2025-05-29 15:39:09
 * @LastEditors: shanghongyun 2879455630@qq.com
 * @LastEditTime: 2025-06-19 15:55:08
 * @FilePath: \A05\User\interrupt.c
 * @Description: 中断函数
 */


#include "NM1120.h"
#include "charger.h"

/**
 * @description: EPWM中断
 * @return {*}
 */
void EPWM_IRQHandler(void)
{
    /* Clear channel 0 period interrupt flag */
    EPWM_ClearPeriodIntFlag(EPWM, 0);
}



Battery_t *pBat_L = &charger.battery_L;
Battery_t *pBat_R = &charger.battery_R;
/**
 * @description: EADC中断
 * @return {*}
 */
void EADC0_IRQHandler(void){
    /* 清除中断标志位 */
    EADC_CLR_INT_FLAG(EADC, EADC_STATUS_ADC0F_Msk);
    uint32_t temp = 0;
    temp = EADC->DAT[0];
    switch(gEADC_Convert_Pos){
        case 0:
            pBat_R->adc_current.data = (temp & EADC_DAT0_ADC0DAT0_Msk) >> EADC_DAT0_ADC0DAT0_Pos;
            if(pBat_R->adc_current.data > pBat_R->current_offset){
                pBat_R->adc_current.data = pBat_R->adc_current.data - pBat_R->current_offset;
            }
            pBat_L->adc_current.data = (temp & EADC_DAT0_ADC1DAT0_Msk) >> EADC_DAT0_ADC1DAT0_Pos;
            if(pBat_L->adc_current.data > pBat_L->current_offset){
                pBat_L->adc_current.data = pBat_L->adc_current.data - pBat_L->current_offset;
            }
            break;

        case 1:
            pBat_L->adc_vout.data = (temp & EADC_DAT0_ADC0DAT0_Msk) >> EADC_DAT0_ADC0DAT0_Pos;
            pBat_R->adc_vout.data = (temp & EADC_DAT0_ADC1DAT0_Msk) >> EADC_DAT0_ADC1DAT0_Pos;
            break;

        case 2:
            // if(pBat_L->state < BAT_Charge_off && pBat_L->state > BAT_IDLE){
            //     if(pBat_L->vol_samp_flag == 1){
            //         pBat_L->adc_voltage.data = (temp & EADC_DAT0_ADC0DAT0_Msk) >> EADC_DAT0_ADC0DAT0_Pos;
            //     }
            // }
            // else{
            //     pBat_L->adc_voltage.data = (temp & EADC_DAT0_ADC0DAT0_Msk) >> EADC_DAT0_ADC0DAT0_Pos;
            // }
            pBat_L->adc_voltage.data = (temp & EADC_DAT0_ADC0DAT0_Msk) >> EADC_DAT0_ADC0DAT0_Pos;
            
            charger.temp.data = (temp & EADC_DAT0_ADC1DAT0_Msk) >> EADC_DAT0_ADC1DAT0_Pos;
            break;

        case 3:
            charger.Vin.data = (temp & EADC_DAT0_ADC0DAT0_Msk) >> EADC_DAT0_ADC0DAT0_Pos;
            pBat_L->adc_current.data = (temp & EADC_DAT0_ADC1DAT0_Msk) >> EADC_DAT0_ADC1DAT0_Pos;
            if(pBat_L->adc_current.data > pBat_L->current_offset){
                pBat_L->adc_current.data = pBat_L->adc_current.data - pBat_L->current_offset;
            }
            break;

        case 4:
            // if(pBat_R->state < BAT_Charge_off && pBat_R->state > BAT_IDLE){
            //     if(pBat_R->vol_samp_flag == 1){
            //         pBat_R->adc_voltage.data = (temp & EADC_DAT0_ADC0DAT0_Msk) >> EADC_DAT0_ADC0DAT0_Pos;
            //     }
            // }
            // else{
            //     pBat_R->adc_voltage.data = (temp & EADC_DAT0_ADC0DAT0_Msk) >> EADC_DAT0_ADC0DAT0_Pos;
            // }
            pBat_R->adc_voltage.data = (temp & EADC_DAT0_ADC0DAT0_Msk) >> EADC_DAT0_ADC0DAT0_Pos;
            
            
            pBat_L->adc_current.data = (temp & EADC_DAT0_ADC1DAT0_Msk) >> EADC_DAT0_ADC1DAT0_Pos;
            if(pBat_L->adc_current.data > pBat_L->current_offset){
                pBat_L->adc_current.data = pBat_L->adc_current.data - pBat_L->current_offset;
            }
            break;

        default:
            break;
    }

    if(++gEADC_Convert_Pos > EADC_CONVERT_POS_MAX){
        gEADC_Convert_Pos = 0;
    }
    EADC->CTL =  (EADC->CTL & EADC_CTL_ADC01CHSEL_MSK) | gEADC_convert_Seq[gEADC_Convert_Pos];

}


void BOD_IRQHandler(void){
    GPIO_SetMode(CHARGE_L_PORT, CHARGE_L_PIN, GPIO_MODE_OPEN_DRAIN);
    GPIO_SetMode(CHARGE_R_PORT, CHARGE_R_PIN, GPIO_MODE_OPEN_DRAIN);
    charger.battery_L.dis_charge();
    charger.battery_R.dis_charge();
}









